const daoUser = require('../../dao/daoUser')


function rr(offset = 0, limit = 1000000, sss) {


  // 把imgs表中的所有url转化成比较短的url，节省内存
  function yyy(row) {
    if (base_url_id_Obj[row.base_url_id] && row.base_url_id) {
      let uu = base_url_id_Obj[row.base_url_id] + row.imgs_url;
      // console.log(``, uu)
      for (let index = 0; index < url_long_to_small.length; index++) {
        const element = url_long_to_small[index];
        // console.log(``, element, uu)
        if (uu.includes(element.base_url)) {
          let imgs_url = uu.replace(element.base_url, '')
          // console.log(``, [imgs_url, element.base_url_id, row.id])
          if (element.base_url_id == row.base_url_id) {
            console.log(`和原来的一样，需要添加新的base_url`, uu);
            c -= 1000;
          } else {
            daoUser.updateImg([imgs_url, element.base_url_id, row.id]).then(d => {
              c++;
              console.log(sss || `====updateImg===   ok  `, n, c, errornul, row.base_url_id, element.base_url_id, row.id, imgs_url)
              // if ((c >= n && n > 50) || (errornul > 0 && n == c + errornul)) {
              //   new Promise(() => { rr(offset, limit); }); console.log(`新的啦`,)
              // };
            }).catch(e => { console.log(`e222`, uu, e.message), errornul++ })
          }
          break;
        }
      }
    }
  }
  // yyy()

  let url_long_to_small = []; // 从长到短的基础url
  let base_url_id_Obj = {};
  let n = 0; let c = 0; let errornul = 0;
  daoUser.getBase_url().then(d => {
    d.forEach(element => { base_url_id_Obj[element.base_url_id] = element.base_url; });
    url_long_to_small = d;
    url_long_to_small.sort((a, b) => b.base_url.length - a.base_url.length);
    // console.log(url_long_to_small, base_url_id_Obj)
    daoUser.getImgsFormSmall(offset, limit).then(d => {
      n = d.length;
      console.log(`length`, n)
      // d.forEach(ele => {
      //   // console.log(`eel`, ele)
      //   yyy(ele);
      // })

      let tou = 0
      let tititi = setInterval(() => {
        if (tou >= n) clearInterval(tititi);
        else yyy(d[tou++]);
      }, 0.01);
    })
    // yyy()
  })

}

rr(0, 100)

// rr(0, 1000000)
// rr(1000000, 1000001,)
// rr(2000000, 1000002,)
// rr(3000000, 1000003,)
// rr(4000000, 1000004,)
// rr(5000000, 1000005,)
// rr(6000000, 1000006,)
// rr(7000000, 1000007,)
// rr(8000000, 1000008,)
// rr(9000000, 1000009,)
// rr(10000000, 1000010,)